home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Tool Chest / Dev.CD Feb 97 TC.toast / Sample Code / Interapplication Communication / MenuScripter 4.0 / Sources / MSAppleEvents.c < prev    next >
Encoding:
Text File  |  1996-07-09  |  8.8 KB  |  279 lines  |  [TEXT/CWIE]

  1. // SVAppleEvents.c
  2. //
  3. // Original version by Jon Lansdell and Nigel Humphreys.
  4. // 4.0 and 3.1 updates by Greg Sutton.
  5. // ©Apple Computer Inc 1996, all rights reserved.
  6.  
  7. #include "MSAppleEvents.h"
  8.  
  9. #include <Resources.h>
  10.  
  11. #include "MSGlobals.h"
  12. #include "MSUtils.h"
  13. #include "MSAEUtils.h"
  14. #include "MSWindow.h"
  15. #include "MSFile.h"
  16. #include "MSMain.h"
  17.  
  18. #include "MSAERecording.h"
  19. #include "MSGXPrinting.h"
  20.  
  21. #include "MSAECompare.h"
  22. #include "MSAECountElements.h"
  23. #include "MSAccessors.h"
  24. #include "MSAECoercions.h"
  25. #include "MSAEObjectsExist.h"
  26. #include "MSAECreate.h"
  27. #include "MSAEClone.h"
  28. #include "MSAEMove.h"
  29. #include "MSAEDelete.h"
  30. #include "MSAECopy.h"
  31. #include "MSAECut.h"
  32. #include "MSAEPaste.h"
  33. #include "MSAESelect.h"
  34. #include "MSAEClose.h"
  35. #include "MSAESave.h"
  36. #include "MSAERevert.h"
  37. #include "MSAEGetData.h"
  38. #include "MSAESetData.h"
  39. #include "MSAEGetDataSize.h"
  40.  
  41. short        gRefNum;
  42.  
  43.  
  44. #pragma segment Main
  45.  
  46. //-----------------------------------------------
  47. //---------- APPLE EVENT HANDLING ---------------
  48. //-----------------------------------------------
  49.  
  50.  
  51. // -----------------------------------------------------------------------
  52. //        Name:             DoAppleEvent
  53. //        Purpose:        Process and despatch the AppleEvent
  54. // -----------------------------------------------------------------------
  55.  
  56. void DoAppleEvent(EventRecord theEvent)
  57. {
  58.     OSErr err;
  59.  
  60.   // should check for your own event message types here - if you have any
  61.     
  62.     err = AEProcessAppleEvent(&theEvent);
  63. }
  64.  
  65. // -----------------------------------------------------------------------
  66. //        Name:             DoOpenApp
  67. //        Purpose:        Called on startup, creates a new document.
  68. // -----------------------------------------------------------------------
  69.  
  70. pascal OSErr DoOpenApp(const AppleEvent *message,const AppleEvent *reply,long refcon)
  71. {
  72. #ifdef __MWERKS__
  73.     #pragma unused (reply,refcon, message)
  74. #endif
  75.  
  76.     DoMenuItem( fileID, fmNew );
  77.  
  78.     return noErr;
  79. }
  80.  
  81. // -----------------------------------------------------------------------
  82. //        Name:             DoOpenDocument
  83. //        Purpose:        Open all the documents passed in the Open AppleEvent.
  84. // -----------------------------------------------------------------------
  85.  
  86. pascal OSErr DoOpenDocument( const AppleEvent *theEvent, const AppleEvent *theReply, long refcon )            
  87. {
  88. #ifdef __MWERKS__
  89.     #pragma unused( theReply, refcon )
  90. #endif
  91.  
  92.     long        index;
  93.     long        itemsInList;
  94.     AEKeyword   keywd;
  95.     AEDescList  docList = { typeNull, NULL };
  96.     long        actSize;
  97.     DescType    typeCode;
  98.     FSSpec      theFSSpec;
  99.     OSErr       anErr;
  100.     
  101.     // Open the specified documents
  102.     
  103.     anErr = AEGetParamDesc( theEvent, keyDirectObject, typeAEList, &docList );
  104.     if ( noErr != anErr ) goto done;
  105.     
  106.     anErr = AECountItems( &docList, &itemsInList );
  107.         
  108.     for ( index = 1; index <= itemsInList; index++ )
  109.     {
  110.         anErr = AEGetNthPtr( &docList, index, typeFSS, &keywd, &typeCode,
  111.                                              (Ptr)&theFSSpec, sizeof( theFSSpec) , &actSize ) ;
  112.         if ( noErr != anErr ) goto done;
  113.  
  114.         anErr = OpenOld( theFSSpec );
  115.         if ( noErr != anErr ) goto done;
  116.     }
  117.  
  118. done:
  119.     (void)AEDisposeDesc( &docList );
  120.         
  121.     return anErr;
  122. }
  123.  
  124. // -----------------------------------------------------------------------
  125. //        Name:             MyQuit
  126. //        Purpose:        Quit event received- exit the program.
  127. // -----------------------------------------------------------------------
  128.  
  129. pascal OSErr MyQuit(const AppleEvent *message,const AppleEvent *reply,long refcon)            
  130. {
  131. #ifdef __MWERKS__
  132.     #pragma unused (reply,refcon)
  133. #endif
  134.     
  135.         DescType     saveOpt;
  136.         DescType     returnedType;
  137.         long         actSize;
  138.         OSErr        anErr;
  139.         
  140.         saveOpt = kAEAsk; // the default
  141.         (void)AEGetParamPtr( message, keyAESaveOptions, typeEnumerated,
  142.                                 &returnedType, (Ptr)&saveOpt, sizeof( saveOpt ), &actSize );
  143.         
  144.         if ( saveOpt != kAENo )
  145.             anErr = AEInteractWithUser( kAEDefaultTimeout, NULL, NULL );
  146.  
  147.       if ( noErr == anErr )
  148.             DoQuit( saveOpt );
  149.         
  150.         return anErr;
  151. }
  152.  
  153.  
  154. // -----------------------------------------------------------------------
  155. //    Name:             DoPrintDocuments
  156. //    Purpose:        Print a list of documents (or windows).
  157. // -----------------------------------------------------------------------
  158.  
  159. pascal OSErr DoPrintDocuments(const AppleEvent *message, AppleEvent *reply, long refcon)
  160. {
  161. #ifdef __MWERKS__
  162.     #pragma unused (reply, refcon)
  163. #endif
  164.  
  165.     long          index;
  166.     long          itemsInList;
  167.     AEKeyword     keywd;
  168.     OSErr         err;
  169.     AEDescList    docList;
  170.     Size          actSize;
  171.     DescType      typeCode;
  172.     FSSpec        theFSSpec;
  173.     WindowToken   theWindowToken;
  174.     OSErr         forgetErr;
  175.     Boolean       talkToUser;
  176.         
  177.     err = AEGetParamDesc(message, keyDirectObject, typeAEList, &docList);
  178.                                              
  179.     err = AECountItems(&docList, &itemsInList);
  180.         
  181.     for (index = 1; index<=itemsInList; index++)
  182.         if (err == noErr) 
  183.         {
  184.             forgetErr = AEGetNthPtr( &docList, index, typeFSS, &keywd,
  185.                                                              &typeCode, (Ptr)&theFSSpec, sizeof(theFSSpec), &actSize);
  186.                                                                 
  187.             talkToUser = false;
  188.  
  189.             if (forgetErr == noErr) 
  190.             {
  191.                 if (err == noErr) 
  192.                     err = IssueAEOpenDoc(theFSSpec);
  193.                     
  194.                 if (err == noErr) 
  195.                     IssuePrintWindow(FrontWindow(), talkToUser);
  196.                     
  197.                 if (err == noErr) 
  198.                     IssueCloseCommand(FrontWindow());
  199.             }
  200.             else
  201.             { // wasn't a file - was it a window ?
  202.                 err = AEGetNthPtr(&docList,
  203.                                     index,
  204.                                     typeMyWndw,
  205.                                     &keywd,
  206.                                     &typeCode,
  207.                                     (Ptr)&theWindowToken,
  208.                                     sizeof(WindowToken),
  209.                                     &actSize);
  210.                                                         
  211.                 if (err == noErr)
  212.                     if (gGXIsPresent)
  213.                         err = GXPrintDocument(DPtrFromWindowPtr(theWindowToken.tokenWindow), talkToUser);
  214.                     else
  215.                         PrintWindow(DPtrFromWindowPtr(theWindowToken.tokenWindow), talkToUser);
  216.             }
  217.         }
  218.     
  219.     if (docList.dataHandle)
  220.         forgetErr = AEDisposeDesc(&docList);
  221.         
  222.     return(err);
  223. } // DoPrintDocuments
  224.  
  225.  
  226. // -----------------------------------------------------------------------
  227. //    Name:             InitAppleEvents
  228. //    Purpose:        Initialise the AppleEvent despatch table
  229. // -----------------------------------------------------------------------
  230.  
  231. void    InitAppleEvents(void)
  232. {
  233.     OSErr aevtErr;
  234.     
  235.     gRefNum = CurResFile();        // Needed for getting application property
  236.     
  237.         // set up the dispatch table for the four standard AppleEvents
  238.     
  239.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(DoOpenApp), noRefCon, false) ;
  240.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,   NewAEEventHandlerProc(DoOpenDocument), noRefCon, false) ;
  241.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,  NewAEEventHandlerProc(DoPrintDocuments), noRefCon, false) ;
  242.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(MyQuit), noRefCon, false) ;
  243.     
  244.         // set up the dispatch table for the core AppleEvents
  245.  
  246.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAESetData,            NewAEEventHandlerProc(DoSetData),   noRefCon, false);
  247.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEGetData,            NewAEEventHandlerProc(DoGetData),   noRefCon, false);
  248.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEGetDataSize,        NewAEEventHandlerProc(DoGetDataSize),   noRefCon, false);
  249.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAECountElements,    NewAEEventHandlerProc(DoCountElements),   noRefCon, false);
  250.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEDoObjectsExist,    NewAEEventHandlerProc(DoObjectsExist),   noRefCon, false);
  251.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAECreateElement,    NewAEEventHandlerProc(DoNewElement),   noRefCon, false);
  252.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEClone,            NewAEEventHandlerProc(DoClone),   noRefCon, false);
  253.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEDelete,            NewAEEventHandlerProc(DoDelete),noRefCon, false);
  254.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEMove,                NewAEEventHandlerProc(DoMove),   noRefCon, false);
  255.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEClose,            NewAEEventHandlerProc(DoCloseWindow),noRefCon, false);
  256.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAESave,                NewAEEventHandlerProc(DoSaveWindow),noRefCon, false);
  257.  
  258.         // set up the dispatch table for the miscellaneous AppleEvents
  259.     
  260.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECut,    NewAEEventHandlerProc(DoCut),   noRefCon, false);
  261.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECopy,   NewAEEventHandlerProc(DoCopy),  noRefCon, false);
  262.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAEPaste,  NewAEEventHandlerProc(DoPaste), noRefCon, false);
  263.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAERevert, NewAEEventHandlerProc(DoRevert),noRefCon, false);
  264.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAESelect, NewAEEventHandlerProc(DoSelect),noRefCon, false);
  265.  
  266.         // Install recording handlers
  267.     aevtErr = InstallRecordingHandlers();
  268.     
  269.         // Install callbacks for count and compare procedures
  270.     aevtErr = InstallObjectCallbacks();
  271.     
  272.         // Now install our object accessors
  273.     aevtErr = InstallAccessors();
  274.  
  275.         // Now the coercion handlers
  276.     aevtErr = InstallCoercions();
  277.  
  278. } // InitAppleEvents
  279.